package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.Admin;
import com.example.entity.RespBean;
import com.example.mapper.AdminMapper;
import com.example.mapper.RequesturlMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2022-01-27
 */
@Api(tags = "AdminController")
@RestController
@RequestMapping("/admin")
public class AdminController  {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RequesturlMapper requesturlMapper;

    @ApiOperation(value = "查询用户名是否存在")
    @PostMapping(value = "/judge-username-exist/{username}")
    public RespBean judgeUsernameExist(@PathVariable String username){
        System.out.println("this username is " + username);
        if(username != null){
            System.out.println("adminMapper.queryUserName(username)=="+adminMapper.queryUserName(username));
            if(adminMapper.queryUserName(username) == 0){
                return RespBean.success("用户名验证成功");
            }else {
                return RespBean.error("用户名已存在，请换一个！");
            }

        }
        return RespBean.error("用户名为空！！！");
    }

    @ApiOperation(value = "创建新用户")
    @PostMapping(value = "/adduser")
    public RespBean addNewUser(@RequestBody Admin admin){
//        System.out.println("this username is " + username);

        adminMapper.addUser(admin.getUsername(),passwordEncoder.encode(admin.getPassword()),admin.getName(),
                admin.getPhone(),admin.getTelephone(),admin.getAddress(), admin.getRemark(), "1", admin.getUser_face());

        adminMapper.addUserRole(adminMapper.queryIdByUserName(admin.getUsername()),901);

        return RespBean.success("新用户创建成功");
    }

    @ApiOperation(value = "查询所有普通用户信息")
    @GetMapping(value = "/queryAllUser")
    public RespBean queryAllUser(){

        List<Admin> adminList = adminMapper.queryAllUser();
        System.out.println("adminList is "+adminList.size());
        for (int i = 0; i < adminList.size(); i++) {
            adminList.get(i).setRoles(requesturlMapper.getRoles(adminList.get(0).getId()));
        }
        if(adminList.size() > 0){
            System.out.println("adminList.size() > 0");
            return RespBean.success("",adminList);
        }else {
            return RespBean.error("没有数据");
        }


    }

    @ApiOperation(value = "禁用用户")
    @PostMapping(value = "/enabledUser/{userid}")
    public RespBean enabledUser(@PathVariable int userid){

        if(userid!=0){
            adminMapper.enabledUser(userid,"0");
//            boolean b = !userDetails.isEnabled();
            return RespBean.success("禁用成功");
        }

        return RespBean.error("禁用失败");
    }
    @ApiOperation(value = "启用用户")
    @PostMapping(value = "/openEnabledUser/{userid}")
    public RespBean openEnabledUser(@PathVariable int userid){

        if(userid!=0){
            adminMapper.enabledUser(userid,"1");
//            boolean b = !userDetails.isEnabled();
            return RespBean.success("启用成功");
        }

        return RespBean.error("启用失败");
    }

    @ApiOperation(value = "删除用户")
    @PostMapping(value = "/del-user/{userid}")
    public RespBean delUserById(@PathVariable int userid){

        adminMapper.delUserById(userid);

        return RespBean.success("用户删除成功");
    }

}
